﻿// Decompiled with JetBrains decompiler
// Type: TaleWorlds.MountAndBlade.View.ItemVisualizer
// Assembly: TaleWorlds.MountAndBlade.View, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 61152EB8-513B-4D4A-8AC0-0D1465C6CF6C
// Assembly location: D:\steam\steamapps\common\Mount & Blade II Bannerlord\Modules\Native\bin\Win64_Shipping_Client\TaleWorlds.MountAndBlade.View.dll

using System.Collections.Generic;
using TaleWorlds.Core;
using TaleWorlds.DotNet;
using TaleWorlds.Engine;
using TaleWorlds.Library;

#nullable disable
namespace TaleWorlds.MountAndBlade.View
{
  public class ItemVisualizer : ScriptComponentBehavior
  {
    private MBGameManager _editorGameManager;
    private bool isFinished;

    protected override void OnEditorInit()
    {
      base.OnEditorInit();
      if (Game.Current != null)
        return;
      this._editorGameManager = (MBGameManager) new EditorGameManager();
    }

    protected override void OnEditorTick(float dt)
    {
      if (this.isFinished || this._editorGameManager == null)
        return;
      this.isFinished = !this._editorGameManager.DoLoadingForGameManager();
      if (!this.isFinished)
        return;
      this.SpawnItems();
    }

    private void SpawnItems()
    {
      Scene scene = this.GameEntity.Scene;
      MBReadOnlyList<ItemObject> objectTypeList = Game.Current.ObjectManager.GetObjectTypeList<ItemObject>();
      Vec3 position = new Vec3(100f, 100f);
      position.z = 2f;
      float num1 = 0.0f;
      float num2 = 200f;
      foreach (ItemObject itemObject in (IEnumerable<ItemObject>) objectTypeList)
      {
        if (itemObject.MultiMeshName.Length > 0)
        {
          MetaMesh copy = MetaMesh.GetCopy(itemObject.MultiMeshName, mayReturnNull: true);
          if ((NativeObject) copy != (NativeObject) null)
          {
            GameEntity empty = GameEntity.CreateEmpty(scene);
            empty.EntityFlags |= EntityFlags.DontSaveToScene;
            empty.AddMultiMesh(copy);
            empty.Name = itemObject.Name.ToString();
            empty.RecomputeBoundingBox();
            float boundingBoxRadius = empty.GetBoundingBoxRadius();
            if ((double) boundingBoxRadius > (double) num1)
              num1 = boundingBoxRadius;
            position.x += boundingBoxRadius;
            if ((double) position.x > (double) num2)
            {
              position.x = 100f;
              position.y += num1 * 3f;
              num1 = 0.0f;
            }
            empty.SetLocalPosition(position);
            position.x += boundingBoxRadius;
            if ((double) position.x > (double) num2)
            {
              position.x = 100f;
              position.y += num1 * 3f;
              num1 = 0.0f;
            }
          }
        }
      }
    }
  }
}
